package com.njzxw.fs.serversys.server;

import com.njzxw.fs.commonutil.web.BaseServer;
import com.njzxw.fs.commonutil.web.ReturnMap;
import com.njzxw.fs.serversys.bean.LogInfoBean;
import com.njzxw.fs.serversys.repository.LogInfoRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class LogInfoServer extends BaseServer{

    @Autowired
    private LogInfoRepository logInfoRepository;

    /**
    * 分页获取数据信息
    *
    * @param request
    * @param response
    * @return
    */
    public ReturnMap queryPage(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean) {
        if(!StringUtils.isBlank(logInfoBean.getOper_date())){
            String[] operDate = logInfoBean.getOper_date().split("至");
            if(operDate.length > 1){
                logInfoBean.setMax_oper_date(operDate[1].trim());
                logInfoBean.setMin_oper_date(operDate[0].trim());
            }
        }
        returnMap.setPageData(logInfoRepository.queryPage(logInfoBean));
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 获取但一条数据信息
     *
     * @param request
     * @param response
     * @return
     */
    public ReturnMap queryId(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean) {
        returnMap.setDataMaps(logInfoRepository.queryId(logInfoBean));
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 修改某一条数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap update(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean) {
        Integer exe = logInfoRepository.update(logInfoBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("修改失败，请稍后再试");
        }
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 删除数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap delete(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean) {
        Integer exe = logInfoRepository.delete(logInfoBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("删除失败，请稍后再试");
        }
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 插入数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap insert(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean) {
        Integer exe = logInfoRepository.insert(logInfoBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("新增失败，请稍后再试");
        }
        returnMap.setDataMaps(logInfoBean.getId());
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 保存操作，根据ID是否存在自动跳转新增还是修改
     * @param request
     * @param response
     * @param logInfoBean
     * @return
     */
    @Transactional
    public ReturnMap save(HttpServletRequest request, HttpServletResponse response,LogInfoBean logInfoBean){
        if(!StringUtils.isBlank(logInfoBean.getId())){
            return this.update(request,response,logInfoBean);
        }else{
            return this.insert(request,response,logInfoBean);
        }
    }

}